#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace Vec
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin()const
		{
			return _start;
		}

		const_iterator end()const
		{
			return _finish;
		}

		vector()
			:_start(nullptr)
			,_finish(nullptr)
			,_endofstage(nullptr)
		{ }

		~vector()
		{
			delete[]_start;
			_start = _finish = _endofstage = nullptr;
		}

		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}

		const T& operator[](size_t pos)const
		{
			assert(pos < size());
			return _start[pos];
		}

		size_t size()
		{
			return _finish - _start;
		}

		size_t capacity()
		{
			return _endofstage - _start;
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t sz = size();
				//size_t cp = capacity() == 0 ? 4 : capacity() * 2;
				T* temp = new T[n];
				if (_start)
				{
					memcpy(temp, _start, sizeof(T) * sz);
					delete[]_start;
				}
				_start = temp;
				_finish = _start + sz;
				_endofstage = _start + n;
			}
		}

		void push_back(const T& x)
		{
			if (_finish == _endofstage)
			{
				reserve(0 ? 4 : capacity() * 2);
			}
			*_finish = x;
			_finish++;
			
		}

	private:
		iterator _start;
		iterator _finish;
		iterator _endofstage;
	};

	void test_vector1()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);
		for (size_t i = 0; i < v.size(); i++)
		{
			cout <<v[i] << " ";
		}
		cout << endl;
		
		vector<int>::iterator it = v.begin();
		while (it != v.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;

		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}
}

//int main()
//{
//	test_vector1();
//	return 0;
//}
